package com.lh.glorypavilion.service.edu.controller.admin;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lh.glorypavilion.result.R;
import com.lh.glorypavilion.service.edu.entity.Course;
import com.lh.glorypavilion.service.edu.entity.Teacher;
import com.lh.glorypavilion.service.edu.entity.form.CourseInfoForm;
import com.lh.glorypavilion.service.edu.entity.vo.CoursePublishVo;
import com.lh.glorypavilion.service.edu.entity.vo.CourseQueryVo;
import com.lh.glorypavilion.service.edu.entity.vo.CourseVo;
import com.lh.glorypavilion.service.edu.service.ICourseService;
import com.lh.glorypavilion.service.edu.service.IVideoService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 课程 前端控制器
 * </p>
 *
 * @author lhui
 * @since 2020-10-12
 */
//@CrossOrigin
@Api(description = "课程管理")
@RestController
@RequestMapping("/admin/edu/course")
@Slf4j
public class CourseController {

    @Autowired
    private ICourseService iCourseService;

    @Autowired
    private IVideoService videoService;

    @ApiOperation("新增课程")
    @PostMapping("save-course-info")
    public R saveCourseInfo(@ApiParam(value = "课程基本信息",required = true) @RequestBody CourseInfoForm courseInfoForm){
        String courseId=iCourseService.saveCourseInfo(courseInfoForm);
        return R.ok().data("courseId",courseId).message("保存成功");
    }

    @ApiOperation("id查询课程")
    @GetMapping("course-info/{id}")
    public R getById(@ApiParam(value = "课程id",required = true) @PathVariable("id") String id){
        CourseInfoForm courseInfoForm=iCourseService.getCourseInfoById(id);
        if(courseInfoForm!=null){
            return R.ok().data("item",courseInfoForm);
        }else {
            return R.ok().message("数据不存在！");
        }
    }

    @ApiOperation("id更新课程")
    @PutMapping("update-course-info")
    public R update(@ApiParam(value = "更新的课程数据",required = true) @RequestBody CourseInfoForm courseInfoForm){
        iCourseService.updateCourseInfoById(courseInfoForm);
        return R.ok().message("更新成功");
    }

    @ApiOperation("课程分页列表")
    @GetMapping("list/{page}/{limit}")
    public R listPage(@ApiParam(value = "当前页码",required = true) @PathVariable("page") Long page,
                      @ApiParam(value = "每页记录数",required = true) @PathVariable("limit") Long limit,
                      @ApiParam(value = "课程列表查询对象（条件）") CourseQueryVo courseQueryVo){
        Page<CourseVo> teacherIPage = iCourseService.selectPage(page, limit, courseQueryVo);
        List<CourseVo> records = teacherIPage.getRecords();
        long total = teacherIPage.getTotal();
        return R.ok().data("total",total).data("rows",records);
    }

    @ApiOperation("id删除课程信息")
    @DeleteMapping("remvoceOne/{id}")
    public R removeOne(@ApiParam("课程ID") @PathVariable("id") String id){

        //删除课程视频
        videoService.removeMediaVidemByCourseId(id);

        //删除封面
        iCourseService.removeCoverById(id);

        //删除课程
        boolean b = iCourseService.removeCourseById(id);
        if(b){
            return R.ok().message("删除成功");
        }
        return R.error().message("数据不存在");
    }

    @ApiOperation("根据ID获取课程发布信息")
    @GetMapping("course-publish/{id}")
    public R getCoursePublishVoById(
            @ApiParam(value = "课程ID", required = true)
            @PathVariable String id){

        CoursePublishVo coursePublishVo = iCourseService.getCoursePublishVoById(id);
        if (coursePublishVo != null) {
            return R.ok().data("item", coursePublishVo);
        } else {
            return R.error().message("数据不存在");
        }
    }

    @ApiOperation("根据id发布课程")
    @PutMapping("publish-course/{id}")
    public R publishCourseById(
            @ApiParam(value = "课程ID", required = true)
            @PathVariable String id){

        boolean result = iCourseService.publishCourseById(id);
        if (result) {
            return R.ok().message("发布成功");
        } else {
            return R.error().message("数据不存在");
        }
    }

}

